package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/6/26 15:51
 * @Description:预约设置
 */


@Service(interfaceClass = OrdersettinService.class)
@Transactional
public class OrdersettinServiceImpl implements OrdersettinService {

    @Autowired
    private OrdersettingDao ordersettingDao;



    /**
     *  @描述：新增预约设置
     */
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            updateNumberByOrderdate(orderSetting);
        }

    }

    @Override
    public List<Map> getOrdersettingData(String date) {
        List<Map> resultList= new ArrayList<>();
        //date 2019-7
        // 生成查询月的第一天的日期字符串 2019-7-1
        String begin = date +"-1";
        // 生成询月的最后一天的日期字符串
        String end = date+"-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> ordersettings= ordersettingDao.getOrdersettingData(map);
        for (OrderSetting ordersetting : ordersettings) {
            Map <String,Integer>  tempMap= new HashMap<>();
            tempMap.put("date",ordersetting.getOrderDate().getDate());
            tempMap.put("number",ordersetting.getNumber());
            tempMap.put("reservations",ordersetting.getReservations());
            resultList.add(tempMap);        }
        return resultList;
    }

    /**
     *  @描述：根据日期修改可预约人数
     */
    public void setData(OrderSetting orderSetting) {
       updateNumberByOrderdate(orderSetting);
    }

    private void updateNumberByOrderdate(OrderSetting orderSetting) {
        //    查找数据库里面是否存在该日期对应的记录
        Long count=ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            //    如果有，更新
            ordersettingDao.update(orderSetting);
        } else {
            //    如果没有，新增
            ordersettingDao.add(orderSetting);
        }
    }
}
